package br.com.mmm.javacenter.framework.view.taglib.helper;

import java.io.IOException;

import javax.persistence.Column;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;

import br.com.mmm.autocenter.fornecedores.entity.Bairro;
import br.com.mmm.autocenter.fornecedores.entity.Cep;
import br.com.mmm.autocenter.fornecedores.entity.Cidade;
import br.com.mmm.autocenter.fornecedores.entity.Cpf;
import br.com.mmm.autocenter.fornecedores.entity.UF;
import br.com.mmm.javacenter.framework.annotations.RenderComponent;
import br.com.mmm.javacenter.framework.reflection.ReflectionFacade;
import br.com.mmm.javacenter.framework.view.ContextAttributes;

@RenderComponent({
	Cpf.class
})
public class InputEndereco extends Input{
	public InputEndereco() {
	
	}
	public InputEndereco(JspContext context, JspFragment fragment) {
		super(context, fragment);
	}
	
	public void addAttributesDefault() throws IOException {
		writer.write(getAtributo("name",name + "_rua"));
		writer.write(getAtributo("id",id + "rua"));
		writer.write(getAtributo("value", rf.get("rua")));
		writer.write(getAtributo("maxLength",9));
		writer.write(getAtributo("size",9));
	}
	@Override
	public void before() throws IOException, JspException {
		Input render = (Input) RenderFactory.getRender(Cep.class);
		render.setContext(getJspContext());
		render.setFragment(getJspBody());
		render.setValue(rf.get("cep"));
		render.setInicioLinha(false);
		render.setInicioLinha(false);
		render.make();
	}
	public void afterInput() throws IOException, JspException  {
		String field = "numero";
		writeLabel(rf.getAttributeGeneric(field,"label"));
		writer.write("<td>");
		writer.write("<input ");
		writer.write(getAtributo("name", getName() + "_numero"));
		writer.write(getAtributo("id",getId() + "_numero"));
		writer.write(getAtributo("value", rf.get(field)));
		Column column = rf.getAttributeField(field,Column.class);
		writer.write(getAtributo("size", column.length()));
		writer.write(getAtributo("maxLength", column.length()));
		writer.write("</td>");
		
		field = "complemento";
		writeLabel(rf.getAttributeGeneric(field,"label"));
		writer.write("<td>");
		writer.write("<input ");
		writer.write(getAtributo("name", getName() + "_numero"));
		writer.write(getAtributo("id",getId() + "_numero"));
		writer.write(getAtributo("value", rf.get(field)));
		column = rf.getAttributeField(field,Column.class);
		writer.write(getAtributo("size", column.length()));
		writer.write(getAtributo("maxLength", column.length()));
		writer.write("</td>");
		
		Input render = (Input) RenderFactory.getRender(Cidade.class);
		render.setContext(getJspContext());
		render.setFragment(getJspBody());
		render.setValue(rf.get("cidade"));
		render.setInicioLinha(false);
		render.setInicioLinha(false);
		render.make();		
		
				
	}
}
